#include <QtGui>

#include "cylinderDialog.h"
#include "src/vector3d.h"
#include "src/color.h"

CylinderDialog::CylinderDialog(int _id, QWidget *_parent)
    : QDialog(_parent), id(_id){

    setWindowTitle(QString::fromUtf8("Novo cilindro"));

    cylinder = NULL;
    colorR = colorG = colorB = colorAlpha = 255;
    centerX = centerY = centerZ = 0.0;
    radius = 1.0;
    height = 1.0;
    rotationX = 0;
    rotationY = 0;
    rotationZ = 0;
    onlyRotated = true;
    rotated = false;
    onlyChangedColor = true;

    createLayout();
}

CylinderDialog::CylinderDialog(CylinderPrimitive *_cylinder, ObjectTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), cylinder(_cylinder), item(_item){

    setWindowTitle(QString::fromUtf8("Modificar objeto ").append(QString::number(_cylinder->getId())));

    const Color &color = cylinder->getColor();
    colorR = color.red * 255;
    colorG = color.green * 255;
    colorB = color.blue * 255;
    colorAlpha = color.alpha * 255;

    Vector3D center = cylinder->getCenter();
    centerX = center.x;
    centerY = center.y;
    centerZ = center.z;

    radius = cylinder->getRadius();
    height = cylinder->getHeight();
    rotationX = 0;
    rotationY = 0;
    rotationZ = 0;
    onlyRotated = true;
    rotated = false;
    onlyChangedColor = true;

    createLayout();
}

CylinderDialog::~CylinderDialog(){
    delete colorPushButton;
}

void CylinderDialog::createLayout(){
    QLabel *centerXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *centerXDoubleSpinBox = new QDoubleSpinBox();
    centerXDoubleSpinBox->setRange(-50.0, 50.0);
    centerXDoubleSpinBox->setSingleStep(1.0);
    centerXDoubleSpinBox->setValue(centerX);
    connect(centerXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterX(double)));


    QLabel *centerYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *centerYDoubleSpinBox = new QDoubleSpinBox();
    centerYDoubleSpinBox->setRange(-50.0, 50.0);
    centerYDoubleSpinBox->setSingleStep(1.0);
    centerYDoubleSpinBox->setValue(centerY);
    connect(centerYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterY(double)));


    QLabel *centerZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *centerZDoubleSpinBox = new QDoubleSpinBox();
    centerZDoubleSpinBox->setRange(-50.0, 50.0);
    centerZDoubleSpinBox->setSingleStep(1.0);
    centerZDoubleSpinBox->setValue(centerZ);
    connect(centerZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterZ(double)));


    QHBoxLayout *centerLayout = new QHBoxLayout;
    centerLayout->addWidget(centerXLabel);
    centerLayout->addWidget(centerXDoubleSpinBox);
    centerLayout->addStretch(1);
    centerLayout->addWidget(centerYLabel);
    centerLayout->addWidget(centerYDoubleSpinBox);
    centerLayout->addStretch(1);
    centerLayout->addWidget(centerZLabel);
    centerLayout->addWidget(centerZDoubleSpinBox);
    centerLayout->addStretch(1);

    QGroupBox *centerGroupBox = new QGroupBox(QString::fromUtf8("Centro"));
    centerGroupBox->setLayout(centerLayout);


//    QLabel *radiusLabel = new QLabel(tr("Raio:"));

    QDoubleSpinBox *radiusDoubleSpinBox = new QDoubleSpinBox();
    radiusDoubleSpinBox->setRange(-50.0, 50.0);
    radiusDoubleSpinBox->setSingleStep(1.0);
    radiusDoubleSpinBox->setValue(radius);
    connect(radiusDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRadius(double)));

    QHBoxLayout *radiusLayout = new QHBoxLayout;
//    radiusLayout->addWidget(radiusLabel);
    radiusLayout->addWidget(radiusDoubleSpinBox);
    radiusLayout->addStretch(1);

    QGroupBox *radiusGroupBox = new QGroupBox(QString::fromUtf8("Raio"));
    radiusGroupBox->setLayout(radiusLayout);


    QDoubleSpinBox *heightDoubleSpinBox = new QDoubleSpinBox();
    heightDoubleSpinBox->setRange(-50.0, 50.0);
    heightDoubleSpinBox->setSingleStep(1.0);
    heightDoubleSpinBox->setValue(height);
    connect(heightDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeHeight(double)));

    QHBoxLayout *heightLayout = new QHBoxLayout;
//    heightLayout->addWidget(heightLabel);
    heightLayout->addWidget(heightDoubleSpinBox);
    heightLayout->addStretch(1);

    QGroupBox *heightGroupBox = new QGroupBox(QString::fromUtf8("Altura"));
    heightGroupBox->setLayout(heightLayout);

    QHBoxLayout *radiusHeightLayout = new QHBoxLayout;
    radiusHeightLayout->addWidget(radiusGroupBox);
    radiusHeightLayout->addWidget(heightGroupBox);



    QLabel *rotateXLabel = new QLabel(QString::fromUtf8("X:"));

    QComboBox *rotateXComboBox = new QComboBox();
    rotateXComboBox->addItem(QString::fromUtf8("0°"));
    rotateXComboBox->addItem(QString::fromUtf8("90°"));
    rotateXComboBox->addItem(QString::fromUtf8("180°"));
    rotateXComboBox->addItem(QString::fromUtf8("270°"));
    rotateXComboBox->setCurrentIndex(rotationX);
    connect(rotateXComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRotationX(int)));

    QLabel *rotateYLabel = new QLabel(QString::fromUtf8("Y:"));

    QComboBox *rotateYComboBox = new QComboBox();
    rotateYComboBox->addItem(QString::fromUtf8("0°"));
    rotateYComboBox->addItem(QString::fromUtf8("90°"));
    rotateYComboBox->addItem(QString::fromUtf8("180°"));
    rotateYComboBox->addItem(QString::fromUtf8("270°"));
    rotateYComboBox->setCurrentIndex(rotationY);
    connect(rotateYComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRotationY(int)));

    QLabel *rotateZLabel = new QLabel(QString::fromUtf8("Z:"));

    QComboBox *rotateZComboBox = new QComboBox();
    rotateZComboBox->addItem(QString::fromUtf8("0°"));
    rotateZComboBox->addItem(QString::fromUtf8("90°"));
    rotateZComboBox->addItem(QString::fromUtf8("180°"));
    rotateZComboBox->addItem(QString::fromUtf8("270°"));
    rotateZComboBox->setCurrentIndex(rotationZ);
    connect(rotateZComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRotationZ(int)));

    QHBoxLayout *rotationLayout = new QHBoxLayout;
    rotationLayout->addWidget(rotateXLabel);
    rotationLayout->addWidget(rotateXComboBox);
    rotationLayout->addStretch(1);
    rotationLayout->addWidget(rotateYLabel);
    rotationLayout->addWidget(rotateYComboBox);
    rotationLayout->addStretch(1);
    rotationLayout->addWidget(rotateZLabel);
    rotationLayout->addWidget(rotateZComboBox);
    rotationLayout->addStretch(1);

    QGroupBox *rotationGroupBox = new QGroupBox(QString::fromUtf8("Rotação"));
    rotationGroupBox->setLayout(rotationLayout);



    colorPushButton = new QPushButton(tr("Cor"));
//    colorPushButton->setFlat(true);
    connect(colorPushButton, SIGNAL(clicked()), this, SLOT(changeColor()));
    setButtonColor();


    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(centerGroupBox);
    mainLayout->addLayout(radiusHeightLayout);
//    mainLayout->addWidget(radiusGroupBox);
//    mainLayout->addWidget(heightGroupBox);
    mainLayout->addWidget(rotationGroupBox);
    mainLayout->addWidget(colorPushButton);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void CylinderDialog::setButtonColor(){
    //    QString colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n" "background-color: rgb(85, 0, 0, 255);");
    QString colorStr;
    if(colorR + colorB + colorG + colorAlpha < 600){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorB));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorAlpha));
    colorStr.append(QString::fromUtf8(");"));

    colorPushButton->setStyleSheet(colorStr);
}

void CylinderDialog::acceptAndCreate(){
    if(!cylinder){
        cylinder = new CylinderPrimitive(Vector3D(centerX, centerY, centerZ), radius, height, Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0), id);
        cylinder->rotate(rotationX, rotationY, rotationZ);
        done(cylinder);

        emit redoOctree(cylinder);
    }else{
        cylinder->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
        cylinder->setCenter(Vector3D(centerX, centerY, centerZ));
        cylinder->setRadius(radius);
        cylinder->setHeight(height);
        cylinder->rotate(rotationX, rotationY, rotationZ);


        if(rotated && onlyRotated){
            emit rotateOctree(cylinder, rotationX, rotationY, rotationZ);
            accept();
            return;
        }

        if(onlyChangedColor){
            accept();
            return;
        }

        QTreeWidgetItem *parent = item->parent();
        if(parent == NULL)
            emit redoOctree(cylinder);
        else{
            while(parent->parent() != NULL){
                parent = parent->parent();
            }
            emit redoOctree(((ObjectTreeItem *)parent)->getObject());
        }
    }

    accept();
}

void CylinderDialog::changeColor(){
    QColor color = QColorDialog::getColor(QColor(colorR, colorG, colorB, colorAlpha), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorR = color.red();
        colorG = color.green();
        colorB = color.blue();
        colorAlpha = color.alpha();
    }
    setButtonColor();
}

void CylinderDialog::changeCenterX(double _x){
    onlyRotated = false;
    onlyChangedColor = false;
    centerX = _x;
}

void CylinderDialog::changeCenterY(double _y){
    onlyRotated = false;
    onlyChangedColor = false;
    centerY = _y;
}

void CylinderDialog::changeCenterZ(double _z){
    onlyRotated = false;
    onlyChangedColor = false;
    centerZ = _z;
}

void CylinderDialog::changeRadius(double _r){
    onlyRotated = false;
    onlyChangedColor = false;
    radius = _r;
}

void CylinderDialog::changeHeight(double _h){
    onlyRotated = false;
    onlyChangedColor = false;
    height = _h;
}

void CylinderDialog::changeRotationX(int _x){
    rotated = true;
    onlyChangedColor = false;
    rotationX = _x;
}

void CylinderDialog::changeRotationY(int _y){
    rotated = true;
    onlyChangedColor = false;
    rotationY = _y;
}

void CylinderDialog::changeRotationZ(int _z){
    rotated = true;
    onlyChangedColor = false;
    rotationZ = _z;
}
